package com.hxkcloud.patient.alarm;

import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

import java.util.Calendar;

/**
 * Created by william_sun on 2016-08-18.
 * Description:
 */
public final class Alarm implements Parcelable {

    public static final Creator<Alarm> CREATOR = new Creator<Alarm>() {
        @Override
        public Alarm createFromParcel(Parcel in) {
            return new Alarm(in);
        }

        @Override
        public Alarm[] newArray(int size) {
            return new Alarm[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(date);
        dest.writeString(time);
        dest.writeInt(hour);
        dest.writeInt(minutes);
        dest.writeInt(isDelay ? 1 : 0);
        dest.writeInt(enabled ? 1 : 0);
//        dest.writeParcelable(alert, flags);
    }

    // 定义列
    public static class Columns implements BaseColumns {
        /**
         * The content:// 为这个表定义一个共享的Url
         */
        public static final Uri CONTENT_URI =
                Uri.parse("content://com.hxkcloud.patient/alarm");
        /**
         * date
         * <P>Type: String</P>
         */
        public static final String DATE = "date";
        /**
         * time
         * <P>Type: String</P>
         */
        public static final String TIME = "time";
        /**
         * Hour in 24-hour localtime 0 - 23.
         * <P>Type: INTEGER</P>
         */
        public static final String HOUR = "hour";

        /**
         * Minutes in localtime 0 - 59
         * <P>Type: INTEGER</P>
         */
        public static final String MINUTES = "minutes";

        /**
         * isdelay 1is 0not
         * <P>Type: BOOLEAN</P>
         */
        public static final String ISDELAY = "isdelay";
        /**
         * True if alarm is active
         * <P>Type: BOOLEAN</P>
         */
        public static final String ENABLED = "enabled";
        /**
         * Audio alert to play when alarm triggers，暂时不用
         * <P>Type: STRING</P>
         *//*
        public static final String ALERT = "alert";
        *//**
         * Message to show when alarm triggers，暂时不用
         * Note: not currently used
         * <P>Type: STRING</P>
         *//*
        public static final String MESSAGE = "message";*/
        /**
         * The default sort order for this table
         */
        public static final String DEFAULT_SORT_ORDER = TIME + " ASC";
        //                HOUR + ", " + MINUTES + " ASC";
        // Used when filtering enabled alarms.
        public static final String WHERE_ENABLED = ENABLED + "=1";
        static final String[] ALARM_QUERY_COLUMNS = {
                _ID, DATE, TIME, HOUR, MINUTES, ISDELAY, ENABLED};

        public static final int ALARM_ID_INDEX = 0;
        public static final int ALARM_DATE_INDEX = 1;
        public static final int ALARM_TIME_INDEX = 2;
        public static final int ALARM_HOUR_INDEX = 3;
        public static final int ALARM_MINUTES_INDEX = 4;
        public static final int ALARM_ISDELAY_INDEX = 5;
        public static final int ALARM_ENABLED_INDEX = 6;
    }

    // 对应的公共的每一列的映射
    public int id;
    public String date;
    public String time;
    public int hour;
    public int minutes;
    public boolean isDelay;
    public boolean enabled;
    public Uri alert;//本地固定一个

    public Alarm(Cursor c) {
        id = c.getInt(Columns.ALARM_ID_INDEX);
        date = c.getString(Columns.ALARM_DATE_INDEX);
        time = c.getString(Columns.ALARM_TIME_INDEX);
        hour = c.getInt(Columns.ALARM_HOUR_INDEX);
        minutes = c.getInt(Columns.ALARM_MINUTES_INDEX);
        isDelay = c.getInt(Columns.ALARM_ISDELAY_INDEX) == 1;
        enabled = c.getInt(Columns.ALARM_ENABLED_INDEX) == 1;

//        alert = Uri.parse(alertString);
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    }

    protected Alarm(Parcel in) {
        id = in.readInt();
        date = in.readString();
        time = in.readString();
        hour = in.readInt();
        minutes = in.readInt();
        isDelay = in.readInt() == 1;
        enabled = in.readInt() == 1;
//        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    }

    // Creates a default alarm at the current time.
    //创建一个默认当前时间的闹钟
    public Alarm() {
        id = -1;
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());
        date = c.get(Calendar.YEAR) + "" + (c.get(Calendar.MONTH) + 1) + "" + c.get(Calendar.DAY_OF_MONTH);
        time = c.getTime().getTime() + "";
        hour = c.get(Calendar.HOUR_OF_DAY);
        minutes = c.get(Calendar.MINUTE);
        isDelay = false;
        enabled = true;
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    }

    //DaysOfWeek
}
